import java.util.HashSet;
import java.util.Set;

public class longestConsecutive {
    public int longestConsecutive(int[] nums) {
        int ans = 0;
        Set<Integer> st = new HashSet<>();
        for(int num : nums){
            st.add(num);
        }
        for(int x : st){
            if(st.contains(x - 1)) continue;//以x-1为起点的一定比x的长
            int y = x + 1;
            while(st.contains(y)) y++;
            ans = Math.max(ans, y - x);
        }
        return ans;
    }
}
